module.exports = new function () {
	var types = {
		proximity: true,
		amenity: true,
		criteria: true
	};
    return {
		list: function(r, listener, failureListener, context) {
			if ( ! r.type) {
				failureListener();
				return;
			}
			if ( ! types[r.type]) {
				failureListener();
                return;
			}
            var collection = context.db.get('types_'+r.type);
            collection.find({}, {}, function(e, docs) {
                if (e) {
                    failureListener(500, e.toString());
                    return;
                }
                listener({ success: true, data: docs });
            });
		},
		save: function(r, listener, failureListener, context) {
			if ( ! r.type) {
				failureListener();
				return;
			}
			if ( ! types[r.type]) {
				failureListener();
                return;
			}
            var value = {
                code: r.code,
                description: r.description,
                priority: r.priority
            };
            var collection = context.db.get('types_'+r.type);

            if (r._id) {
                value._id = r._id;
                collection.update({ _id: r._id }, value, function (e, doc) {
                    if (e) {
                        failureListener(500, e.toString());
                        return;
                    }
                    listener({ success: !!doc, data: value});
                });
            } else {
                collection.insert(value, function (e, doc) {
                    if (e) {
                        failureListener(500, e.toString());
                        return;
                    }
                    listener({ success: true, data: doc });
                });
            }
		},
		remove: function(r, listener, failureListener, context) {
            if ( ! r._id && ! r.code) {
                failureListener(404);
				return;
            }
            if ( ! r.type) {
				failureListener();
				return;
			}
			if ( ! types[r.type]) {
				failureListener();
                return;
			}
            var remove = {};
            if (r._id) {
                remove._id = r._id;
            } else {
                remove.code = r.code;
            }
            var collection = context.db.get('types_'+r.type);
            collection.remove(remove, { justOne: true }, function(e) {
                listener({
                    success: !e
                });
            });
		},
		get: function(r, listener, failureListener, context) {
            if ( ! r._id && ! r.code) {
                failureListener(404);
				return;
            }
			if ( ! r.type) {
				failureListener();
				return;
			}
			if ( ! types[r.type]) {
				failureListener();
                return;
			}
            var get = {};
            if (r._id) {
                get._id = r._id;
            } else {
                get.code = r.code;
            }
            var collection = context.db.get('types_'+r.type);
			collection.find({}, {}, function(e, docs) {
                if (e) {
                    failureListener(500, e.toString());
                    return;
                }
                var el = null;
                if (docs.length > 0) {
                    el = docs[0];
                }
                listener({ success: true, data: el });
            });
		}
	};
};